<%@ page contentType="text/html;charset=UTF-8" language="java"
         pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html lang="zh-CN">
<%@include file="inculd-head.jsp" %>
<%--抽取分页操作为独立的js文件--%>
<link rel="stylesheet" href="css/pagination.css"/>
<script type="text/javascript" src="jquery/jquery.pagination.js"></script>
<link rel="stylesheet" href="ztree/zTreeStyle.css"/>
<script type="text/javascript" src="ztree/jquery.ztree.all-3.5.min.js"></script>
<script type="text/javascript" src="crowd/my-role.js"></script>
<script type="text/javascript" src="crowd/role-auth.js"></script>
<script src="layer/layer.js" charset="UTF-8"></script>
<script type="text/javascript">
    $(function () {
        //    为分页操作初始化数据
        window.pageNum = 1;
        window.pageSize = 10;
        window.keyword = "";
        //调用执行分页函数,显示分页效果
        generatePage();
        //给搜索按钮绑定单机响应函数
        $("#searchBtn").click(function () {
            //获取关键词数据赋值给全局变量
            window.keyword = $("#keywordInput").val();
            window.pageNum = 1;
            //刷新页面方法对页面进行更新
            generatePage();
        });
        //弹出模态框
        $("#showAddModalBtn").click(function () {
            $("#addModal").modal("show");
        });

        //模态框保存的单击事件
        $("#saveRoleBtn").click(function () {

            //获取用户输入的角色名称并去空格
            //#addModal整个模态框
            //空格表示在后代元素中继续查找
            //[name=roleName]匹配name属性等于roleName的元素
            var roleName = $.trim($("#addModal [name=roleName]").val());
            //发送ajax请求
            $.ajax({
                "url": "role/save.json",
                "type": "post",
                "data": {
                    "name": roleName
                },
                "dataType": "json",
                "success": function (response) {
                    var result = response.result;
                    if (result == "SUCCESS") {
                        layer.msg("操作成功");
                        //跳到最后一页
                        window.pageNum = 999999;
                        //重新加载分页数据
                        generatePage();
                    }
                    if (result == "FAILED") {
                        layer.msg("操作失败" + response.message);
                    }

                },
                "error": function (response) {
                    layer.msg(response.status + " " + response.statusText);
                }
            });
            //关闭模态框
            $("#addModal").modal("hide");
            //清理模态框
            $("#addSuccess3").val("");
        });

        /**
         * 实现每一行的修改图标绑定单击事件
         * 关于on函数的说明：
         * 第一个参数——响应事件
         * 第二个参数——选择器
         * 第三个参数——响应函数体
         */
        $("#rolePageBody").on("click",".pencilBtn",function () {
            //打开模态框
            $("#editModal").modal("show");
            //获取角色名
            var roleName = $(this).parent().prev().text();
            //获取角色id
            window.roleId = this.id;
            //用户名填入输入框中
            $("#editModal [name=roleName]").val(roleName);
            //给模态框中的更新按钮绑定单击响应事件
            $("#updateRoleBtn").click(function () {
                var roleName = $("#editSuccess3").val();
                $.ajax({
                   "url":"role/update.json",
                    "type": "post",
                    "data": {
                       "id":window.roleId,
                        "name": roleName
                    },
                    "dataType": "json",
                    "success": function (response) {
                        var result = response.result;
                        if (result == "SUCCESS") {
                            layer.msg("操作成功");
                            //重新加载分页数据
                            generatePage();
                        }
                        if (result == "FAILED") {
                            layer.msg("操作失败" + response.message);
                        }
                    },
                    "error": function (response) {
                        layer.msg(response.status + " " + response.statusText);
                    }
                });
                //关闭模态框
                $("#editModal").modal("hide");
            });
        });

        //执行模态框中的确认删除操作
        $("#removeRoleBtn").click(function () {
            //取出存入全局变量的id数组
            var requestBody = JSON.stringify(window.roleIdArray);
            $.ajax({
                "url":"role/remove/by/role/id/array.json",
                "type":"post",
                "data":requestBody,
                "contentType":"application/json;charset=UTF-8",
                "dataType":"json",
                "success": function (response) {
                    var result = response.result;
                    if (result == "SUCCESS") {
                        layer.msg("操作成功");
                        //重新加载分页数据
                        generatePage();
                    }
                    if (result == "FAILED") {
                        layer.msg("操作失败" + response.message);
                    }
                },
                "error": function (response) {
                    layer.msg(response.status + " " + response.statusText);
                }
            });
            //关闭模态框
            $("#confimModal").modal("hide");
        });

        /**
         * 单条的删除按钮绑定单击响应函数
         * 和之前的修改一样，删除操作也是需要用on()绑定的，因为是动态生成的代码
         */
        $("#rolePageBody").on("click",".removeBtn",function () {
            //从表单取出roleName
            var roleName = $(this).parent().prev().text();
            //准备roleArray对象数组
            var roleArray = [{
                roleId:this.id,
                name:roleName
            }];
            //调用函数打开模态框，js中做了封装，就不用再写了
            showConfirmModal(roleArray);
        });

        /**
         * 给全选框绑定单击响应函数
         */
        $("#summaryBox").click(function () {
            //获取当前多选框是否被选中
            var currentStatus = this.checked;
            //用多选框目前的选中状态去控制单选框
            $(".itemBox").prop("checked",currentStatus);
        });
        /**
         * 反向操作：单选框控制复选框的状态
         */
        $("#rolePageBody").on("click",".itemBox",function () {
            //获取被选中的单选框数量和总的单选框数量
            var checkedBoxCount = $(".itemBox:checked").length;
            var totalBoxCount = $(".itemBox").length;
            //如果全部被选中，则全选也被选中
            $("#summaryBox").prop("checked",checkedBoxCount == totalBoxCount);
        });

        /**
         * 给批量删除按钮绑定单击响应函数
         */
        $("#batchRemoveBtn").click(function () {
            var roleArray = [];
            //对于每个选中的box，就用each函数直接遍历
            //遍历出的对象是页面上的checkboxTd对象本身
            $(".itemBox:checked").each(function () {
                //引用每个遍历对象的id属性
                var roleId = this.id;
                //DOM获取角色名
                var roleName = $(this).parent().next().text();
                //把遍历出的角色名和id封装成对象加入数组
                roleArray.push({
                   "roleId":roleId,
                   "name":roleName
                });
            });
            //检查roleArray的长度
            if (roleArray.length == 0){
                layer.msg("至少需要选择一个角色");
                return ;
            }
            //调用封装好的删除确认模态框函数
            showConfirmModal(roleArray);
        });

        //编辑角色按钮的单击事件
        $("#rolePageBody").on("click",".checkBtn",function () {
            // 点击按钮把请求中的id传入全局变量的roleId
            window.roleId = this.id;
            // 打开模态框
            $("#assignModal").modal("show");

            fillAuthTree();
        });

        // 给分配权限模态框中的提交按钮绑定单击函数
        $("#assignBtn").click(function () {
            //获取树形结构的节点中被勾选的节点
            var authIdArray = [];
            // 获取zTree中被勾选的节点集合
            var zTreeObj = $.fn.zTree.getZTreeObj("authTreeDemo");
            var checkedNodes = zTreeObj.getCheckedNodes();
            for (var i = 0; i < checkedNodes.length; i++) {
                var checkedNode = checkedNodes[i];
                authIdArray.push(checkedNode.id);
            }

            //发送提交请求，保存到role_auth表中
            var requestBody = {
                "authIdArray":authIdArray,
                //roleId也放进数组，服务器端可以统一使用List<Integer>方式接收数据
                "roleId":[window.roleId]
            };

            requestBody = JSON.stringify(requestBody);

            $.ajax({
                "url":"assign/do/role/assign/auth.json",
                "type":"post",
                "data":requestBody,
                "contentType":"application/json;charset=UTF-8",
                "dataType":"json",
                "success": function (response) {
                    var result = response.result;
                    if (result == "SUCCESS") {
                        layer.msg("操作成功");
                    }
                    if (result == "FAILED") {
                        layer.msg("操作失败" + response.message);
                    }
                },
                "error": function (response) {
                    layer.msg(response.status + " " + response.statusText);
                }
            });
            $("#assignModal").modal("hide");
        });
    });
</script>
<body>
<%@include file="inculd-nav.jsp" %>
<div class="container-fluid">
    <div class="row">
        <%@include file="include-sidebar.jsp" %>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title"><i class="glyphicon glyphicon-th"></i> 数据列表</h3>
                </div>
                <div class="panel-body">
                    <form class="form-inline" role="form" style="float:left;">
                        <div class="form-group has-feedback">
                            <div class="input-group">
                                <div class="input-group-addon">查询条件</div>
                                <input id="keywordInput" class="form-control has-success" type="text"
                                       placeholder="请输入查询条件">
                            </div>
                        </div>
                        <button id="searchBtn" type="button" class="btn btn-warning"><i
                                class="glyphicon glyphicon-search"></i> 查询
                        </button>
                    </form>
                    <button id="batchRemoveBtn" type="button" class="btn btn-danger" style="float:right;margin-left:10px;"><i
                            class=" glyphicon glyphicon-remove"></i> 删除
                    </button>
                    <button type="button" id="showAddModalBtn" class="btn btn-primary" style="float:right;"><i
                            class="glyphicon glyphicon-plus"></i> 新增
                    </button>
                    <br>
                    <hr style="clear:both;">
                    <div class="table-responsive">
                        <table class="table  table-bordered">
                            <thead>
                            <tr>
                                <th width="30">#</th>
                                <th width="30"><input id="summaryBox" type="checkbox"></th>
                                <th>名称</th>
                                <th width="100">操作</th>
                            </tr>
                            </thead>
                            <tbody id="rolePageBody"><%--在这用jq生成删除的操作--%></tbody>
                            <tfoot>
                            <tr>
                                <td colspan="6" align="center">
                                    <div id="Pagination" class="pagination"><!-- 这里显示分页 --></div>
                                </td>
                            </tr>
                            </tfoot>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<%@ include file="modal-role-add.jsp" %>
<%@ include file="modal-role-edit.jsp" %>
<%@ include file="modal-role-confim.jsp" %>
<%@ include file="modal-role-assign-auth.jsp"%>
</body>
</html>